<?php 
	session_start();
        require_once '../funciones.php';
        $con = con_caweb();
//
//
//
//    $soporte=$_SESSION['caweb']['soporte'];
//
//	include("dbconfig.php");
        $fechaIni   = $_REQUEST['fechaini'];
        $fechaFin   = $_REQUEST['fechaFin'];
        $nit        = $_REQUEST['nit'];
        $factura    = $_REQUEST['factura'];
        $sucursal   = $_REQUEST['sucursal'];
        //Se construye la condicion
        $where = ($nit != "")?" and fac.cliente = '$nit'":"";
        $where = ($factura != "")?" and fac.factura = '$factura'":"";
        $where = ($sucursal != "")?" and fac.sucursal = '$sucursal'":"";



	$page = $_REQUEST['page']; 

	// get how many rows we want to have into the grid - rowNum parameter in the grid 
	$limit = $_REQUEST['rows']; 

	// get index row - i.e. user click to sort. At first time sortname parameter -
	// after that the index from colModel 
	$sidx = $_REQUEST['sidx']; 

	// sorting order - at first time sortorder 
	$sord = $_REQUEST['sord']; 

	// if we not pass at first time index use the first column for the index or what you want
	if(!$sidx) $sidx =1; 

//	// connect to the MySQL database server
//	$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die("Connection Error: " . mysql_error());
//
//	// select the database
//	mysql_select_db($database) or die("Error connecting to db.");

	// calculate the number of rows for the query. We need this for paging the result 
        $sql = "select fac.facturasistem,fac.sucursal,fac.fechapago,fac.factura,fac.fechafactura,fac.base,fac.iva,fac.total,fac.nombre,fac.retefuente_base,fac.reteiva_base,fac.reteica_base,fac.otros,fac.cuadre_base,fac.valorpagado,(fac.valorpagado - fac.iva) as recaudoBase,fac.observacioncartera,
        fac.estadocartera,rec.tipo,rec.banco,rec.cuenta,rec.cheque from cabeceras_fac fac
                inner join abonosfacturacion abo on abo.facturasistema = fac.facturasistema and abo.recaudo <> 0 and abo.fechaabono in (select max(aboaux.fechaabono) from abonosfacturacion aboaux where aboaux.facturasistema = fac.facturasistema group by aboaux.facturasistema)
                inner join recaudos rec on rec.recaudo = abo.recaudo
                where fac.factura <> 0 and fac.estado != 'A' and fac.fechapago between '$fechaIni' and '$fechaFin'".$where;
	$result = mysql_query($sql,$con) or die("Error SQL 1".  mysql_error().$sql);
	$row = mysql_fetch_array($result,MYSQL_ASSOC); 
	$count = $row['count']; 

	// calculate the total pages for the query 
	if( $count > 0 ) { 
				  $total_pages = ceil($count/$limit); 
	} else { 
				  $total_pages = 0; 
	} 

	// if for some reasons the requested page is greater than the total 
	// set the requested page to total page 
	if ($page > $total_pages) $page=$total_pages;

	// calculate the starting position of the rows 
	$start = $limit*$page - $limit;

	// if for some reasons start position is negative set it to 0 
	// typical case is that the user type 0 for the requested page 
	if($start <0) $start = 0; 

	// the actual query for the grid data
//	$SQL = "SELECT * FROM abonosfacturacion WHERE transaccion > 0 GROUP BY transaccion ORDER BY $sidx $sord LIMIT $start , $limit";

	$SQL = $sql ." ORDER BY $sidx $sord LIMIT $start , $limit";

	$result = mysql_query( $SQL ) or die("Error SQL 2.".mysql_error().$SQL);

	// we should set the appropriate header information
	if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
				  header("Content-type: application/xhtml+xml;charset=utf-8"); 
	} else {
			  header("Content-type: text/xml;charset=utf-8");
	}
	echo "<?xml version='1.0' encoding='utf-8'?>";
	echo "<rows>";
	echo "<page>".$page."</page>";
	echo "<total>".$total_pages."</total>";
	echo "<records>".$count."</records>";

	// be sure to put text data in CDATA
	while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
		echo "<row id='". $row['facturasistema']."'>";
		echo "<cell>". $row['sucursal']."</cell>";
		echo "<cell>". $row['fechapago']."</cell>";
		echo "<cell>". $row['factura']."</cell>";
                echo "<cell>". $row['fechafactura']."</cell>";
                echo "<cell>". utf8_encode(htmlspecialchars($row['nombre']))."</cell>";
                echo "<cell>". number_format($row['base'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['iva'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['total'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['retefuente_base'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['reteiva_base'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['reteica_base'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['otros'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['cuadre_base'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['valorpagado'],0, ',', '.')."</cell>";
                echo "<cell>". number_format($row['recaudoBase'],0, ',', '.')."</cell>";
                echo "<cell>". utf8_encode(htmlspecialchars($row['observacioncartera']))."</cell>";
                echo "<cell>". $row['estadocartera']."</cell>";
                echo "<cell>". $row['tipo']."</cell>";
                echo "<cell>". $row['banco']."</cell>";
                echo "<cell>". $row['cuenta']."</cell>";
                echo "<cell>". $row['cheque']."</cell>";
		echo "</row>";
	}
	echo "</rows>";
	 
?>
